using System.Collections;
using Holoville.HOTween;
using Holoville.HOTween.Core;
using UnityEngine;

public class FlyingPoints : MonoBehaviour
{
	public UISprite sprite;

	public UILabel text;

	public Animation anim;

	private FlyingPointsInfo flyingPointsInfo;

	private Vector3 originPosition;

	private Vector3 destinationPosition;

	private bool worldToGUI;

	private IEnumerator callBack;

	private Vector3 screenCenter = new Vector3((float)Screen.width / 2f, (float)Screen.height / 2f, 0f);

	public void Init(FlyingPointsInfo flyingPointsInfo, Vector3 origin, Vector3 destination, bool worldToGUI, IEnumerator callBack)
	{
		HOTween.Init();
		this.flyingPointsInfo = flyingPointsInfo;
		this.callBack = callBack;
		this.worldToGUI = worldToGUI;
		originPosition = origin;
		destinationPosition = destination;
		sprite.spriteName = flyingPointsInfo.iconSpriteName;
		sprite.color = flyingPointsInfo.color;
		text.text = string.Empty;
		AnimationStart();
		FlyingPointsController.instance.PlaySound(flyingPointsInfo.startSound1, flyingPointsInfo.volumeMultiplier);
	}

	private void AnimationStart()
	{
		if (worldToGUI)
		{
			originPosition = WorldToUI(originPosition);
			base.transform.localPosition = originPosition;
		}
		else
		{
			base.transform.position = originPosition;
		}
		base.transform.localScale = Vector3.zero;
		StartCoroutine(FlyAnimation(flyingPointsInfo.withAnimation));
	}

	private IEnumerator FlyAnimation(bool playAnim)
	{
		if (playAnim)
		{
			if (Random.Range(1, 10) > 5)
			{
				anim.Play("FlyingPointsAnimation_1");
			}
			else
			{
				anim.Play("FlyingPointsAnimation_2");
			}
		}
		HOTween.To(base.transform, flyingPointsInfo.flyTime / 5f, new TweenParms().Prop("localScale", Vector3.one).UpdateType(UpdateType.TimeScaleIndependentUpdate));
		HOTween.To(base.transform, flyingPointsInfo.flyTime, new TweenParms().Prop("position", destinationPosition).UpdateType(UpdateType.TimeScaleIndependentUpdate).Ease(EaseType.EaseInOutExpo)
			.OnComplete((TweenDelegate.TweenCallback)delegate
			{
				Remove();
			}));
		yield return StartCoroutine(CoroutineRunner.WaitForSeconds(flyingPointsInfo.flyTime - flyingPointsInfo.flyTime / 5f));
		HOTween.To(base.transform, flyingPointsInfo.flyTime / 4f, new TweenParms().Prop("localScale", Vector3.zero).UpdateType(UpdateType.TimeScaleIndependentUpdate));
	}

	private Vector3 WorldToUI(Vector3 position)
	{
		Vector3 result = PointerController.instance.mainCamera.WorldToScreenPoint(position);
		if (result.z < 0f)
		{
			result.x *= -1f;
			result.y *= -1f;
		}
		result -= screenCenter;
		result *= (float)PointerController.instance.uiRoot.activeHeight / (float)Screen.height;
		result.z = 0f;
		return result;
	}

	private void Remove()
	{
		FlyingPointsController.instance.PlaySound(flyingPointsInfo.endSound1, flyingPointsInfo.volumeMultiplier);
		if (callBack != null)
		{
			StartCoroutine(callBack);
		}
		Object.Destroy(base.gameObject);
	}
}
